<?php
App::import('Vendor', 'twitter', array('file'=>'twitter'.DS.'TwitterApi.php'));
Configure::write('debug', 0);

class TwitterController extends AppController{
	var $uses = array('User');
	var $consumer_key = "si2UvZAvFqJULHPSReXjA";
	var $consumer_secret = "qn4YEDOFUPYTwWkexlk2SdDYcW7zAvRaPNSR5GJ5k";
	var $components = array('RequestHandler','Fauth');
	
	function beforeFilter() {
		//$this->datahash['twitter_user'] = $this->Session->read('twitter_user');
		$this->Fauth->allow(array('api'));
		// セッションまたはクッキーにユーザーIDがあるとき
		if($this->Fauth->isAuth()){
			$this->datahash['login_user'] = $this->Fauth->user();
		}
		// セッションまたはクッキーにユーザーIDがないとき
		else{
			if(!$this->Fauth->isAllowAction()){
				if($this->Fauth->canmove()){
					echo json_encode(array("frexi error" => "not auth"));
					exit();
				}
			}
		}
		//Ajaxの共通処理
		//アクションにajaxが入っていたら実行
		//if( $this->RequestHandler->isAjax()){
			//jquery-mobileはすべてajaxなので例外も入れてやる
		Configure::write('debug', 0);
		$this->layout = "";
		$this->autoRender = false;
		
		//}
	}
		
	function api(){
		$method = empty($this->params['form']['method']) ? 0 : $this->params['form']['method'];
		$action = empty($this->params['form']['action']) ? 10 : $this->params['form']['action'];
		unset($this->params['form']['method']);
		unset($this->params['form']['action']);
		unset($this->params['form']['screen_name']);
		$param = $this->params['form'];
		
		$api = new TwitterApi(
			$this->consumer_key, 
			$this->consumer_secret, 
			$this->datahash['login_user']['User']['access_token_key'], 
			$this->datahash['login_user']['User']['access_token_secret']
		);
		header("Content-Type: text/javascript; charset=utf-8"); 
		echo $api->requestApi($action, $method, $param);
		exit();
	}
}
?>